﻿using GeneticAlgorithms;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using GeneticAlgorithms.Genomes;
using System.Linq;
using System;
using GeneticAlgorithms.Populations;
using Moq;
using System.Threading;

namespace GeneticAlgorithms.Tests
{


    /// <summary>
    ///This is a test class for GeneticAlgorithmTest and is intended
    ///to contain all GeneticAlgorithmTest Unit Tests
    ///</summary>
    [TestClass()]
    public class GeneticAlgorithmTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void EvolveTest()
        {
            int generationCount = 10;
            Func<BinaryGenome, double> calculateFitness =
                (genome => genome.Sum(gene => gene ? 1 : 0));

            GAConfigurationBase config = new GAConfigurationBase(100, 100, 2000, false, null);

            Mock<IPopulation<ArrayGenome<bool>, bool>> populationMock =
                new Mock<IPopulation<ArrayGenome<bool>, bool>>(MockBehavior.Strict);

            populationMock.Expect(p => p.Initialize());
            populationMock.Expect(p => p.UpdateStats());
            populationMock.Expect(p => p.Generations).Returns(1);
            populationMock.Expect(p => p.AverageFitness).Returns(.5);
            populationMock.Expect(p => p.BestFitness).Returns(1);
            populationMock.Expect(p => p.Variance).Returns(1.0);
            populationMock.Expect(p => p.Count).Returns(1);
            populationMock.Expect(p => p.GenerationVariance).Returns(1.0);

            GeneticAlgorithm<ArrayGenome<bool>, bool> ga =
                new GeneticAlgorithm<ArrayGenome<bool>, bool>(
                    populationMock.Object, config);

            for (int i = 0; i < generationCount; ++i)
            {
                populationMock.Expect(p => p.DoStep());
                populationMock.Expect(p => p.Generations).Returns(1);
                populationMock.Expect(p => p.AverageFitness).Returns(.5);
                populationMock.Expect(p => p.BestFitness).Returns(1);
                populationMock.Expect(p => p.Variance).Returns(1.0);
                populationMock.Expect(p => p.Count).Returns(1);
                populationMock.Expect(p => p.GenerationVariance).Returns(1.0);
            }

            ga.Evolve();

            populationMock.VerifyAll();
        }

        [TestMethod()]
        public void DoStepTest()
        {
            Func<BinaryGenome, double> calculateFitness =
                (genome => genome.Sum(gene => gene ? 1 : 0));

            Mock<IPopulation<ArrayGenome<bool>, bool>> populationMock =
                new Mock<IPopulation<ArrayGenome<bool>, bool>>(MockBehavior.Strict);

            populationMock.Expect(p => p.Initialize());
            populationMock.Expect(p => p.UpdateStats());
            populationMock.Expect(p => p.DoStep());
            populationMock.Expect(p => p.Generations).Returns(1);
            populationMock.Expect(p => p.AverageFitness).Returns(.5);
            populationMock.Expect(p => p.BestFitness).Returns(1);
            populationMock.Expect(p => p.Variance).Returns(1.0);
            populationMock.Expect(p => p.Count).Returns(1);
            populationMock.Expect(p => p.GenerationVariance).Returns(1.0);

            GeneticAlgorithm<ArrayGenome<bool>, bool> ga =
                new GeneticAlgorithm<ArrayGenome<bool>, bool>(
                    populationMock.Object);

            ga.DoStep();

            populationMock.VerifyAll();
        }

        [TestMethod]
        public void CheckStopTestByGeneration()
        {
            GeneticAlgorithm target = new GeneticAlgorithm(10, 10, foo);

            GeneticAlgorithm_Accessor accessor = new GeneticAlgorithm_Accessor(
                new PrivateObject(target));
            accessor.Configuration.MaxGenerations = 100;
            accessor.Status.Generation = 101;
            Assert.IsTrue(accessor.CheckStop());
        }

        [TestMethod]
        public void CheckStopTestByBestFitness()
        {
            GeneticAlgorithm target = new GeneticAlgorithm(10, 10, foo);

            GeneticAlgorithm_Accessor accessor = new GeneticAlgorithm_Accessor(
                new PrivateObject(target));

            accessor.Configuration.DesiredFitness = 100;
            accessor.Status.BestFitness = 215;
            Assert.IsTrue(accessor.CheckStop());
        }

        [TestMethod]
        public void CheckStopTestByGenerationVariance()
        {
            GeneticAlgorithm target = new GeneticAlgorithm(10, 10, foo);

            GeneticAlgorithm_Accessor accessor = new GeneticAlgorithm_Accessor(
                new PrivateObject(target));
            accessor.Configuration.CalculateVariance = true;
            accessor.Configuration.MinGenerationVariance = .1;
            accessor.Status.GenerationVariance = .0123;
            Assert.IsTrue(accessor.CheckStop());
        }

        private double foo(BinaryGenome g)
        {
            return 0;
        }
    }
}
